package com.shsun.curveservice;

import com.shsun.curveservice.domain.entity.InterfaceUri;
import com.shsun.curveservice.logic.service.InterfaceUriService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

@Component
public class EndpointPrinter implements CommandLineRunner {

    private final RequestMappingHandlerMapping handlerMapping;

    @Resource
    private InterfaceUriService interfaceUriService;

    @Autowired
    public EndpointPrinter(ApplicationContext applicationContext) {
        this.handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
    }

    @Override
    public void run(String... args) {
        //  获取RequestMappingHandlerMapping中的url与方法映射
        Map<RequestMappingInfo,  HandlerMethod>  handlerMethods  =  handlerMapping.getHandlerMethods();

        for  (Map.Entry<RequestMappingInfo,  HandlerMethod>  entry  :  handlerMethods.entrySet())  {
            RequestMappingInfo  requestMappingInfo  =  entry.getKey();
            HandlerMethod  handlerMethod  =  entry.getValue();
            Method method  =  handlerMethod.getMethod();

            PathPatternsRequestCondition pathPatternsCondition = requestMappingInfo.getPathPatternsCondition();
            if (pathPatternsCondition == null) {
                continue;
            }

            //  获取接口路径
            Set<String> patterns = requestMappingInfo.getPathPatternsCondition().getPatternValues();
            String  url  =  String.join(",",  patterns);
            //  获取接口访问方式
            String[]  methods  =  requestMappingInfo.getMethodsCondition().getMethods().stream().map(Enum::name).toArray(String[]::new);
            String  type  =  String.join("  ",  methods);  //  使用空格作为分隔符


            InterfaceUri interfaceUri = new InterfaceUri();
            interfaceUri.setUrl(url);
            interfaceUri.setMethod(type);
//            interfaceUriService.save(interfaceUri);
            interfaceUriService.saveIgnore(interfaceUri);



            //  获取接口参数列表
            Class<?>[]  parameterTypes  =  method.getParameterTypes();

            System.out.println("Path:  "  +  patterns);
            System.out.println("Methods:  "  +  String.join(",  ",  methods));
            System.out.print("Parameters:  ");
            for  (Class<?>  parameterType  :  parameterTypes)  {
                System.out.print(parameterType.getSimpleName()  +  "  ");
            }
            System.out.println();
        }

    }
}
